﻿using AntUnion.FrameWork.Model.Models;
using AntUnion.FrameWork.Model.ModelsExt;
using Microsoft.AspNetCore.Mvc;
using static AntUnion.FrameWork.Web.Service.ReportServer;
using System.Collections.Generic;
using System;
using AntUnion.FrameWork.Web.Common;
using Microsoft.EntityFrameworkCore;
using System.Threading.Tasks;
using GSF.Units;
using Microsoft.CodeAnalysis;
using TencentCloud.Cpdp.V20190820.Models;
using static AntUnion.FrameWork.Web.Service.ReferralService;
using AntUnion.FrameWork.Util;
using AntUnion.FrameWork.Web.Service;
using System.Linq;

namespace AntUnion.FrameWork.Web.Controllers
{
    /// <summary>
    /// 推广控制器
    /// </summary>
    public class ReferralController : BaseController
    {
        /// <summary>
        /// 推广中心
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
            int oneClients = 0;
            int towClients = 0;
            referralService.GetClients(crmUserModel.Id, out oneClients, out towClients);
            ViewBag.oneClients = oneClients;
            ViewBag.towClients = towClients;
            CrmUser crmUser = crmService.GetCrmUser(crmUserModel.Id);
            ViewBag.role = crmUser.Role;
            List<PublicService.SystemConfigModel> systemConfigModels = publicService.GetSystemConfig();
            string baseUrl = systemConfigModels.FirstOrDefault(k => k.name == "weburl").value;
            string url = baseUrl + "/Login/Register?p=" + SecurityUtil.Encrypt($"group={crmUser.CrmGroup}&up={crmUser.Id}");
            ViewBag.url = url;
            return View();
        }

        /// <summary>
        /// 代理申请
        /// </summary>
        /// <returns></returns>
        public IActionResult Application()
        {
            CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
            Promotion promotion = referralService.GetApplication(crmUserModel.Id);
            meunService.UpdateRead(crmUserModel.Id, 3);
            return View(promotion);
        }

        /// <summary>
        /// 代理申请
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult SendApplication(string cont)
        {
            Result res = new Result();
            try
            {
                CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
                if (crmUserModel == null)
                {
                    return NotFound();
                }
                if (string.IsNullOrEmpty(cont))
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.contNull;
                    return new JsonResult(res);
                }
                CrmUser crmUser = crmService.GetCrmUser(crmUserModel.Id);
                Promotion promotion = referralService.GetApplication(crmUserModel.Id);
                if (promotion != null)
                {
                    if (promotion.State == 0 || promotion.State == 2)
                    {
                        res.Code = 1;
                        res.Msg = LanguageConfig.hasPromotion;
                        return new JsonResult(res);
                    }
                }
                int count = referralService.SendApplication(crmUserModel.Id, cont);
                if (count > 0)
                {
                    res.Code = 0;
                    res.Msg = LanguageConfig.submitSuccess;
                    return new JsonResult(res);
                }
                else
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.submitFail;
                    return new JsonResult(res);
                }
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Msg = ex.Message;
                return new JsonResult(res);
            }
        }

        /// <summary>
        /// 个人报表
        /// </summary>
        /// <returns></returns>
        public IActionResult PersonReport()
        {
            return View();
        }

        /// <summary>
        /// 获取账户列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetPersonReport(int page, int limit, string alllName, string registrationTime, string balanceDate)
        {
            ListResult res = new ListResult();
            try
            {
                int count = 0;
                CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
                //List<dynamic> list = reportServer.GetOrders(page, limit, null, null, crmUserModel.Id, order, symbol, state, timeSetup, out count);
                List<PersonReportModel> list = referralService.PersonReport(page, limit, crmUserModel.Id, alllName, registrationTime, balanceDate, out count);
                res.Code = 0;
                res.Count = count;
                res.Data = list;
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Data = ex.Message;
            }
            return new JsonResult(res);
        }

        /// <summary>
        /// 明细报表
        /// </summary>
        /// <returns></returns>
        public IActionResult CommissionDetailedReport()
        {
            return View();
        }

        [HttpGet]
        public IActionResult GetCommissionDetailedReportSymbol()
        {
            Result res = new Result();
            try
            {
                List<string> list = referralService.CommissionDetailedReportSymbol();
                res.Code = 0;
                res.Data = list;
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Data = ex.Message;
            }
            return new JsonResult(res);
        }

        /// <summary>
        /// 获取账户列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetCommissionDetailedReport(int page, int limit, string alllName, string symbol, string rebateTime, string balanceDate)
        {
            ListResult res = new ListResult();
            try
            {
                int count = 0;
                CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
                //List<dynamic> list = reportServer.GetOrders(page, limit, null, null, crmUserModel.Id, order, symbol, state, timeSetup, out count);
                List<DailyDetailedReportModel> list = referralService.DailyDetailedReport(page, limit, crmUserModel.Id, alllName, symbol, rebateTime, balanceDate, out count);
                res.Code = 0;
                res.Count = count;
                res.Data = list;
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Data = ex.Message;
            }
            return new JsonResult(res);
        }

        #region 下线持仓

        /// <summary>
        /// 持仓
        /// </summary>
        /// <returns></returns>
        public IActionResult Position()
        {
            return View();
        }

        [HttpGet]
        public IActionResult GetDownPositionList(int page, int limit, string server, string group, string alllName, string position, string symbol, string enPositionAction, string time)
        {
            ListResult res = new ListResult();
            try
            {
                int count = 0;
                CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
                List<PositionList> list = reportServer.GetDownPosition(page, limit, null, null, crmUserModel.Id, alllName, position, symbol, enPositionAction, time, out count);
                res.Code = 0;
                res.Count = count;
                res.Data = list;
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Data = ex.Message;
            }
            return new JsonResult(res);
        }

        #endregion

        #region 下线交易

        /// <summary>
        /// 交易
        /// </summary>
        /// <returns></returns>
        public IActionResult Deal()
        {
            return View();
        }

        [HttpGet]
        public IActionResult GetDownDealList(int page, int limit, string server, string group, string alllName, string order, string symbol, string dealAction, string entry, string time)
        {
            ListResult res = new ListResult();
            try
            {
                int count = 0;
                CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
                List<DealList> list = reportServer.GetDownDeals(page, limit, null, null, crmUserModel.Id, alllName,order, symbol, dealAction, entry, time, out count);
                res.Code = 0;
                res.Count = count;
                res.Data = list;
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Data = ex.Message;
            }
            return new JsonResult(res);
        }

        #endregion

        #region 下线订单

        /// <summary>
        /// 订单
        /// </summary>
        /// <returns></returns>
        public IActionResult Order()
        {
            return View();
        }

        [HttpGet]
        public IActionResult GetDownOrderList(int page, int limit, string alllName,string order, string symbol, string type, string time)
        {
            ListResult res = new ListResult();
            try
            {
                int count = 0;
                CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
                //List<dynamic> list = reportServer.GetOrders(page, limit, null, null, crmUserModel.Id, order, symbol, state, timeSetup, out count);
                List<OrderList> list = reportServer.GetDownOrders(page, limit, null, null, crmUserModel.Id, alllName,order, symbol, type, time, out count);
                res.Code = 0;
                res.Count = count;
                res.Data = list;
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Data = ex.Message;
            }
            return new JsonResult(res);
        }

        #endregion

        #region 下级客户

        public IActionResult LevelOneUser()
        {
            return View();
        }

        [HttpGet]
        public IActionResult GetLevelOneUser(int page, int limit, string registerTime)
        {
            ListResult res = new ListResult();
            try
            {
                int count = 0;
                CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
                List<LevelUserModel> list = referralService.GetLevelOneUser(page, limit, crmUserModel.Id, registerTime, out count);
                res.Code = 0;
                res.Count = count;
                res.Data = list;
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Data = ex.Message;
            }
            return new JsonResult(res);
        }

        public IActionResult LevelTwoUser()
        {
            return View();
        }

        [HttpGet]
        public IActionResult GetLevelTwoUser(int page, int limit, string registerTime)
        {
            ListResult res = new ListResult();
            try
            {
                int count = 0;
                CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
                List<LevelUserModel> list = referralService.GetLevelTwoUser(page, limit, crmUserModel.Id, registerTime, out count);
                res.Code = 0;
                res.Count = count;
                res.Data = list;
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Data = ex.Message;
            }
            return new JsonResult(res);
        }


        #endregion
    }
}
